/*
 * Copyright (C) 2012 haru Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.harusource.di.core.view;

import com.harusource.di.core.HaruCore;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;

public abstract class HaruFrameLayout extends FrameLayout {
    public HaruFrameLayout(Context context) {
        super(context);
        if (!isInEditMode())
            HaruCore.init(context.getApplicationContext());
    }

    public HaruFrameLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        if (!isInEditMode())
            HaruCore.init(context.getApplicationContext());
    }

    public HaruFrameLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        if (!isInEditMode())
            HaruCore.init(context.getApplicationContext());
    }

    @Override
    protected final void onFinishInflate() {
        super.onFinishInflate();
        inject(getContext());
        onFinishInflateLayout();
    }

    private void inject(Context context) {
        if (!isInEditMode()) {
            Context ctx = context.getApplicationContext();
            HaruCore.init(ctx);
            HaruCore.injectEvent(ctx, this);
            HaruCore.injectFeild(ctx, this);
        }
    }

    @Override
    public final void addView(View child) {
        super.addView(child);
        inject(getContext());
    }

    @Override
    public final void addView(View child, int width, int height) {
        super.addView(child, width, height);
        inject(getContext());
    }

    @Override
    public final void addView(View child, int index, android.view.ViewGroup.LayoutParams params) {
        super.addView(child, index, params);
        inject(getContext());
    }

    @Override
    public final void addView(View child, int index) {
        super.addView(child, index);
        inject(getContext());
    }

    @Override
    public final void addView(View child, android.view.ViewGroup.LayoutParams params) {
        super.addView(child, params);
        inject(getContext());
    }

    @Override
    protected final boolean addViewInLayout(View child, int index,
            android.view.ViewGroup.LayoutParams params, boolean preventRequestLayout) {
        boolean ret = super.addViewInLayout(child, index, params, preventRequestLayout);
        inject(getContext());
        return ret;
    }

    @Override
    protected final boolean addViewInLayout(View child, int index,
            android.view.ViewGroup.LayoutParams params) {
        boolean ret = super.addViewInLayout(child, index, params);
        inject(getContext());
        return ret;
    }

    protected void onFinishInflateLayout() {
    };
}
